package mrusanov.sounds

import java.util.concurrent.Executors

object SoundPlayer {
	private val SOUNDS_ENABLED = false
	private val THREAD_POOL_SIZE = 5
	val threadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE)

	def playSound(path: String) {
		if (!SOUNDS_ENABLED) {
			return ;
		}

		threadPool.submit(createTask(path))
	}

	private def createTask(path: String): Runnable = {
		val lowercase = path.toLowerCase
		val renderer = if (lowercase.endsWith("wav")) {
			new WaveRenderer
		} else if (lowercase.endsWith("ogg")) {
			new JOrbisOggRenderer
		} else if (lowercase.endsWith("mp3")) {
			new JavaLayerMp3Renderer
		} else {
			throw new IllegalArgumentException("Renderer for path " + path + " is not found!")
		}

		new Runnable {
			def run() {
				renderer.play(getClass.getResource(path))
			}
		}
	}

}
